# encoding:utf-8
import json
import re
import requests
from bs4 import BeautifulSoup


from Career import Career
from Hero import Hero
from Soldier import Soldier
from Talent import Talent
from Transfer import Transfer


# #region careers

# careers = []
# careerSoup = BeautifulSoup(requests.get("https://wiki.biligame.com/langrisser/分类:职业").text, "html.parser")
# careersNum = re.search(r"共(\d+)个页面", careerSoup.select("#mw-pages > p")[0].text, re.MULTILINE).group(1)
# pageLink = f"""https://wiki.biligame.com{careerSoup.select("#mw-pages > a")[1].attrs["href"]}"""
# for i in range(int((int(careersNum)+200-1)/200)):
#     careerSoup = BeautifulSoup(requests.get(pageLink).text, "html.parser") if i != 0 else careerSoup
#     pageLink = f"""https://wiki.biligame.com{careerSoup.select("#mw-pages > a")[1].attrs["href"]}"""
#     all_careers = careerSoup.select("#mw-pages > div > div")[0].select("a")
#     for j in all_careers:
#         careerName = j.text
#         careerInfoSoup = BeautifulSoup(requests.get(f"https://wiki.biligame.com/langrisser/index.php?title={careerName}&action=edit").text, "html.parser")
#         careerInfo = careerInfoSoup.select("#wpTextbox1")[0].text
#         careers.append(Career(careerInfo))
#         print(f"职业: {careerName}")


# with open("careers.json", "w", encoding="utf-8") as f:
#     f.write(json.dumps(careers, ensure_ascii=False, indent=2, sort_keys=False, separators=(',', ': ')))

# #endregion


# #region heroes&transfers

# heroes = []
# transfers = []
# heroSoup = BeautifulSoup(requests.get("https://wiki.biligame.com/langrisser/分类:英雄").text, "html.parser")
# all_heroes = heroSoup.select("#mw-pages > div > div")[0].select("a")
# for i in all_heroes:
#     heroName = i.text
#     if heroName[:2] == "马修":
#         continue
#     heroInfoSoup = BeautifulSoup(requests.get(f"https://wiki.biligame.com/langrisser/index.php?title={'杰利奥鲁%26蕾拉' if heroName == '杰利奥鲁&蕾拉' else heroName}&action=edit").text, "html.parser")
#     heroInfo = heroInfoSoup.select("#wpTextbox1")[0].text
#     hero = Hero(heroInfo)
#     for i in range(8):
#         transfer = Transfer(heroInfo, hero["英雄名"], i)
#         hero["转职路线"].append(transfer["职业"])
#         transfers.append(transfer)
#     heroes.append(hero)
#     print(f"英雄: {heroName}")

# with open("heroes.json", "w", encoding="utf-8") as f:
#     f.write(json.dumps(heroes, ensure_ascii=False, indent=2, sort_keys=False, separators=(',', ': ')))
# with open("transfers.json", "w", encoding="utf-8") as f:
#     f.write(json.dumps(transfers, ensure_ascii=False, indent=2, sort_keys=False, separators=(',', ': ')))

# #endregion


# #region talents

# talents = []
# talentSoup = BeautifulSoup(requests.get("https://wiki.biligame.com/langrisser/index.php?title=分类:天赋&action=edit&redlink=1").text, "html.parser")
# all_talents = talentSoup.select("#mw-pages > div > div")[0].select("a")
# for i in all_talents:
#     talentName = i.text
#     talentInfoSoup = BeautifulSoup(requests.get(f"https://wiki.biligame.com/langrisser/index.php?title={talentName}&action=edit").text, "html.parser")
#     talentInfo = talentInfoSoup.select("#wpTextbox1")[0].text
#     talents.append(Talent(talentInfo))
#     print(f"天赋: {talentName}")

# with open("talents.json", "w", encoding="utf-8") as f:
#     f.write(json.dumps(talents, ensure_ascii=False, indent=2, sort_keys=False, separators=(',', ': ')))

# #endregion

# region soldier

soldiers = []
soldierSoup = BeautifulSoup(
    requests.get(
        "https://wiki.biligame.com/langrisser/index.php?title=分类:兵种&action=edit&redlink=1"
    ).text,
    "html.parser",
)
all_soldiers = soldierSoup.select("#mw-pages > div > div")[0].select("a")
for i in all_soldiers:
    soldierName = i.text
    if soldierName == "模板:兵种图鉴":
        continue
    soldierInfoSoup = BeautifulSoup(
        requests.get(
            f"https://wiki.biligame.com/langrisser/index.php?title={soldierName}&action=edit"
        ).text,
        "html.parser",
    )
    soldierInfo = soldierInfoSoup.select("#wpTextbox1")[0].text
    soldiers.append(Soldier(soldierInfo))
    print(f"兵种: {soldierName}")

with open("soldiers.json", "w", encoding="utf-8") as f:
    f.write(
        json.dumps(
            soldiers,
            ensure_ascii=False,
            indent=2,
            sort_keys=False,
            separators=(",", ": "),
        )
    )

# endregion


print("OK")
